//
//  ViewController.swift
//  Calculator
//
//  Created by Gavin YANG on 2/3/15.
//  Copyright (c) 2015 HDIC. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var label_Display: UILabel!
    
    var displayValue: Double {
        get {
            return NSNumberFormatter().numberFromString(label_Display.text!)!.doubleValue
        }
        
        set {
            label_Display.text = "\(newValue)"
        }
    }
    
    var userIsInMiddleInputANumber = false
    var brain = CalculatorBrain()
   
    @IBAction func button_AppendNumber(sender: UIButton) {
        let digit = sender.currentTitle!
        
        if (userIsInMiddleInputANumber) {
            label_Display.text = label_Display.text! + digit
        } else {
            label_Display.text = digit
            userIsInMiddleInputANumber = true;
        }
    }

    @IBAction func button_Enter() {
        userIsInMiddleInputANumber = false
        
        if let result = brain.pushOperand(displayValue) {
            displayValue = result
        } else {
            displayValue = 0
        }
    }
 
    @IBAction func button_Operate(sender: UIButton) {
        if userIsInMiddleInputANumber {
            button_Enter()
        }
        
        if let operate = sender.currentTitle {
            if let result = brain.performOperation(operate) {
                displayValue = result
            } else {
                displayValue = 0
            }
        }
    }
}

